/*
* Class: RssFeed
* Description: Class RssFeed represents an RSS Feed and contains RSS feed elements.
*
* Note:
* Date: March 28, 2012
* Author: Shane Swayney
*/

package rssreaderapp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;


/**
 *Class RssFeed represents an RSS Feed.  
 * 
 * @author Shane Swayney <shane.swayney@gmail.com>
 */
public class RssFeed implements Serializable{
    
    //Class variables and containers
    private String rssURL = "";
    private String rssTitle = "";
    private String lastModified = "";
    
    //Used to store the relevent components of an rss feed: channels and items 
    private ArrayList<ItemComponent> feedComponentsArrayList = new ArrayList<>(0);
    
    
 
    /**
     * Constructor to create an RssFeed from an rss feed's url
     * 
     * @param urlString A string containing the URL of the RssFeed
     */
    public RssFeed(String urlString)
    {
        this.rssURL = urlString;
    }


    /**
     * Method returns an Iterator to be used to sequencialy get {@link ItemComponent}s 
     * 
     * @return iterator<itemComponent>
     */
    public Iterator<ItemComponent> getFeedComponentsIterator() {
        return feedComponentsArrayList.iterator();
    }
    

    /**
     * Method to create and add an {@link ItemComponent} to this RssFeed object using the parameters provided
     * 
     * @param itemTitle The title of the {@link ItemComponent} to be added
     * @param itemLink The link to the items web page
     */
    public void addItemComponent(String itemTitle, String itemLink)
    {
        ItemComponent item = new ItemComponent(itemTitle,itemLink);
        feedComponentsArrayList.add(item);
    }
    
    
    /**
     * Method to create and add an {@link ChannelComponent} to this RssFeed object using the parameters provided
     * 
     * @param itemTitle The title of the {@link ChannelComponent} to be added
     * @param itemLink The link to the main web page of the site providing the feed
     */
    public void addChannelComponent(String itemTitle, String itemLink)
    {
        ItemComponent channel = new ChannelComponent(itemTitle,itemLink);
        feedComponentsArrayList.add(channel);
    }
    
    
    /**
     * Method to search and return the url of the corresponding component title
     * 
     * @param titleString The title of the component
     * @return A String containing the url, or an empty String if not found
     */
    public String getLinkByComponentTitle(String titleString)
    {
        //search for the title and return its url if in feedComponentArrayList
        for(ItemComponent item: feedComponentsArrayList)
        {
            if(item.getItemTitle().equalsIgnoreCase(titleString))
            {
                //title found return its url link
                return item.getItemURL();
            }
        }
      //title not found return empty string  
      return "";
    }
    


    /**
     * Method to return the URL to the rss feed 
     * 
     * @return the String rssURL 
     */
    public String getRssURL() {
        return rssURL;
    }

    /**
     * Method sets the rss feed url to be used
     * 
     * @param rssURL the rssURL to set
     */
    public void setRssURL(String rssURL) {
        this.rssURL = rssURL;
    }

    /**
     * Method returns a string containing the rss feed's title
     * 
     * @return the rssTitle
     */
    public String getRssTitle() {
        return rssTitle;
    }

    /**
     * Sets the title of the rss feed
     * 
     * @param rssTitle the rssTitle to set
     */
    public void setRssTitle(String rssTitle) {
        this.rssTitle = rssTitle;
    }

    /**
     * Returns a string containing the date this feeds url was last updated.
     * This information is from the header file info when then the rss is created
     * and updated. see {@link RssFeedFactory.createRssFeed()}. This string is 
     * used to evaluated the need for this feed to be updated.
     * 
     * @return A String containing the lastModified date of the rss feed site 
     */
    public String getLastModified() {
        return lastModified;
    }

    /**
     * Method to set the last Modified date of the url feed taken from the
     * url files header
     * @param lastModified the lastModified to set
     */
    public void setLastModified(String lastModified) {
        this.lastModified = lastModified;
    }
    
    @Override
    public String toString()
    {
        return rssTitle;
    }
    
    
    
}


